﻿using AutoMapper;
using B.S.AuthCenter.Api.Read.Application.Command.MenuRole;
using B.S.AuthCenter.Api.Read.DTOs.RoleMenu;
using B.S.AuthCenter.Domain;
using B.S.AuthCenter.ErrorCount;
using B.S.AuthCenter.InfrasIntructure.Interface;
using MediatR;

namespace B.S.AuthCenter.Api.Read.Application.Handler.MenuRole
{
    public class GetRoleSelectHandler : IRequestHandler<GetRoleSelectCommand, ApiResult<List<RoleSelectDTO>>>
    {
        IBaseRepository<RoleModel> roleRepo;
        IMapper mapper;

        public GetRoleSelectHandler(IBaseRepository<RoleModel> roleRepo, IMapper mapper)
        {
            this.roleRepo = roleRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<RoleSelectDTO>>> Handle(GetRoleSelectCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<RoleSelectDTO>> res=new ApiResult<List<RoleSelectDTO>>();
            res.Datas = GetRoleSelect();
            res.Code=ApiEnum.Success;
            res.Msg = "查询角色下拉框成功";
            return Task.FromResult(res);

        }
        public List<RoleSelectDTO> GetRoleSelect()
        {
            var role=roleRepo.GetAll().Where(c=>!c.IsDeleted).ToList();
            var model=mapper.Map<List<RoleSelectDTO>>(role);
            return model;
        }
    }
}
